package controller;

import enity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.StudentService;

import java.util.List;

@RequestMapping("/student")
@Controller
public class StudentController {
    @Autowired
    StudentService studentService;

    @RequestMapping("/add.do")
    public ModelAndView doAddStudent(Student student) {
        ModelAndView modelAndView = new ModelAndView();

        String tips = "";
        int res = studentService.addStudent(student);

        tips = res > 0 ? "【" + student.getName() + "】插入成功！" : "插入失败";

        modelAndView.addObject("tips", tips);

        modelAndView.setViewName("show");
        return modelAndView;
    }


    @RequestMapping("queryStudent.do")
    public ModelAndView queryStudent() {
        ModelAndView modelAndView = new ModelAndView();
        List<Student> students = studentService.selectStudents();
        modelAndView.addObject("students", students);
        modelAndView.setViewName("studentsPage");
        return modelAndView;
    }
}
